﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

/// <summary>
/// Point类的扩展方法类
/// </summary>
public static class PointExtensions
{
    /// <summary>
    /// 将点p(x,y)转成平面向量
    /// </summary>
    /// <param name="p">平面点</param>
    /// <returns>平面向量</returns>
    public static Vector ToVector(this Point p)
    {
        return new Vector(p.X, p.Y);
    }
    
    /// <summary>
    /// 将点p(x,y)按指定平面向量平移
    /// </summary>
    /// <param name="p">平面点</param>
    /// <param name="v">指定的平面向量</param>
    /// <returns>平移后的点</returns>
    public static Point Offset(this Point p, Vector v)
    {
        return new Point(p.X+v.X, p.Y+v.Y);
    }

    /// <summary>
    /// 根据转换矩阵，旋转点
    /// </summary>
    /// <param name="m"></param>
    /// <param name="p"></param>
    /// <returns></returns>
    public static Point Rotate(this Matrix m, Point p)
    {
        return new Point((m.M11 * p.X) + (m.M12 * p.Y), (m.M21 * p.X) + (m.M22 * p.Y));
    }

    /// <summary>
    /// 点是否位于指定矩形范围内
    /// </summary>
    /// <param name="point"></param>
    /// <param name="rect"></param>
    /// <returns></returns>
    public static bool IsWithinBoundsOf(this Point point, Rect rect)
    {
        //is the x coordiate within the rectangle bound
        if (point.X > rect.Left && point.X < rect.Y)
        {
            //is the y coordinate within the rectangle bounds
            if (point.Y > rect.Top && point.Y < rect.Bottom)
            {
                return true;
            }
        }
        return false;
    }
}